package com.orangelife.mobile.complaints.activity;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.curry.android.util.DialogHelper;
import com.curry.orangelife.mobile.R;
import com.orangelife.mobile.common.activity.OrangeLifeBaseActivity;
import com.orangelife.mobile.complaints.adapter.MyHousesAdapter;
import com.orangelife.mobile.complaints.biz.Complaints;
import com.orangelife.mobile.constants.Constant;
import com.orangelife.mobile.util.JsonReponseHandler;
import com.orangelife.mobile.util.ToastHelper;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by luxiaojuan
 * on 2015/11/16.
 */
public class MyHouseListActivity extends OrangeLifeBaseActivity {

    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.lvMyHouse)
    ListView lvMyHouse;

    private MyHousesAdapter<Map<String,Object>> adapter=null;
    private List<Map<String,Object>> listmap;

    private Dialog dialog;

    Complaints complaints = Complaints.getInstance();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_house_list);
        ButterKnife.bind(this);

        initView();
        findView();
    }

    /**
     * 初始化
     */
    private void initView() {
        tvTitle.setText("我的房屋");
    }

    private void findView() {
        Map<String, Object> map = new HashMap<>();
        setAdapter();

        lvMyHouse.setOnItemClickListener(onItemClickListener);

        dialog = DialogHelper.getInstance().createLoadingDialog(MyHouseListActivity.this,
                Constant.LOADING);
        dialog.show();
        complaints.getMyHousesList(map, new MyHouseHandler(MyHouseListActivity.this));
    }

    /**
     * 设置适配器
     */
    private void setAdapter() {

        listmap = new ArrayList<Map<String, Object>>();
//        adapter = new MyHousesAdapter<Map<String, Object>>(
//                this, listmap);

        lvMyHouse.setAdapter(adapter);
    }

    AdapterView.OnItemClickListener onItemClickListener = new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
            Intent intent=new Intent();
            intent.putExtra("commID", String.valueOf(listmap.get(position).get("commID")));
            intent.putExtra("apartmentID", String.valueOf(listmap.get(position).get("apartmentID")));
            intent.putExtra("houseInfo", String.valueOf(listmap.get(position).get("houseInfo")));
            intent.putExtra("phone", String.valueOf(listmap.get(position).get("phone")));
            setResult(2, intent);
            finish();
        }
    };

    static class MyHouseHandler extends Handler {
        private WeakReference<MyHouseListActivity> myHouseListActivity = null;

        public MyHouseHandler(MyHouseListActivity activity) {
            myHouseListActivity = new WeakReference<>(activity);
        }

        @Override
        public void handleMessage(Message msg) {
            MyHouseListActivity act = myHouseListActivity.get();

            if (act != null) {
                switch (msg.what) {
                    case 500:
                        ToastHelper.getInstance()._toast(msg.obj.toString());
                        DialogHelper.closeDialog(act.dialog);
                        break;
                    case 1001:
                        ToastHelper.getInstance()._toast(msg.obj.toString());
                        act.isLogin(act.dialog);
                        break;
                    case 2:
                        act.dialog.dismiss();
                        act.listmap = JsonReponseHandler.getListFromJson(
                                String.valueOf(msg.obj), act.adapter, act.dialog);
                        break;
                    default:
                        break;
                }
                if (act.dialog != null) {
                    act.dialog.dismiss();
                }
            }
        }
    }
}
